import Vue from 'vue'
import VueRouter from 'vue-router'
// import Home from '../views/Home.vue'

Vue.use(VueRouter)

import login from '../components/Login.vue'
import nav from '../components/Nav.vue'
import kinds from '../components/goodsManage/Kinds.vue'
import shoplist from '../components/goodsManage/ShopList.vue'
import ordermanage from '../components/orderManage/OrderManage.vue'
import permise from '../components/permiseManage/Permise.vue'
import roles from '../components/permiseManage/Roles.vue'

// import a from '../components/date'
// import a from '../components/goodsManage'
// import a from '../components/orderManage'
// import a from '../components/permiseManage'
// import a from '../components/userManage'

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: login },
  {
    path: '/nav',
    component: nav,
    children: [
      { path: '/kinds', name: 'kinds', component: kinds },
      { path: '/shoplist', name: 'shoplist', component: shoplist },
      { path: '/ordermanage', name: 'ordermanage', component: ordermanage },
      { path: '/permise', name: 'permise', component: permise },
      { path: '/roles', name: 'roles', component: roles },
    ]
  },
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫,to表示将要访问的路径，from表示从哪里来，next是下一个要做的操作 next('/login')强制跳转logi
router.beforeEach((to, from, next) => {
  // 访问登录页，放行
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  // 没有token, 强制跳转到登录页
  if (!tokenStr) return next('/login')
  next()
})

export default router
